package org.paradigmshift.timelapse.ui
import java.awt.Component
import java.awt.Container
import java.awt.FocusTraversalPolicy
import java.awt.Insets
import scala.swing.event.Key
import scala.swing.event.KeyPressed
import scala.swing.Action
import scala.swing.BoxPanel
import scala.swing.Button
import scala.swing.Dialog
import scala.swing.GridBagPanel
import scala.swing.Label
import scala.swing.Orientation
import scala.swing.PasswordField
import scala.swing.Swing
import scala.swing.TextField
import org.paradigmshift.timelapse.rest.JsonRestConnection
import javax.swing.JOptionPane
import org.paradigmshift.timelapse.TimeLapsePreferences

class LoginDialog extends Dialog {
  
  private val usernameField = new TextField {
    columns = 15
    text = TimeLapsePreferences.getUsername
  }
  private val passwordField = new PasswordField { columns = 15 }
  private val okButton = new Button {
    action = Action( "OK" ) {
      passwordField.password
      if ( usernameField.text != "" && passwordField.password.length > 0 ) {
        JsonRestConnection.setAuth( usernameField.text, new String( passwordField.password ) )
        close
      }
      else {
        JOptionPane.showMessageDialog( this.peer, "Username and password must not be empty!", "Error", JOptionPane.ERROR_MESSAGE )
      }
    }
  }
  private val cancelButton = new Button {
    action = Action( "Cancel" ) {
      JsonRestConnection.clearAuth
      close
    }
  }
  
  private val panel = new GridBagPanel() {
    border = Swing.EmptyBorder( 7 )
    
    private val c = new Constraints
    c.fill = GridBagPanel.Fill.Both
    c.anchor = GridBagPanel.Anchor.NorthWest
    
    c.grid = ( 0, 0 )
    layout( new Label( "Username: " ) ) = c
    c.gridy = 1
    layout( new Label( "Passowrd: " ) ) = c
    
    c.grid = ( 1, 0 )
    c.gridwidth = 2
    layout( usernameField ) = c
    c.gridy = 1
    layout( passwordField ) = c
    
    c.anchor = GridBagPanel.Anchor.Center
    c.grid = ( 0, 2 )
    c.gridwidth = 3
    c.insets = new Insets( 5, 0, 0, 0 )
    layout( new BoxPanel( Orientation.Vertical ) {
      contents += new BoxPanel( Orientation.Horizontal ) {
        contents += okButton
        contents += cancelButton
      }
    }) = c
  }
  
  peer.setFocusTraversalPolicy( new FocusTraversalPolicy() {
    val focusList:List[Component] = List( usernameField.peer, passwordField.peer, okButton.peer, cancelButton.peer )
    var focus = 0
    def getComponentAfter( focusCycleRoot:Container, component:Component ) = {
      focus = (focus + 1) % focusList.length
      if ( !focusList( focus ).isEnabled ) getComponentAfter( focusCycleRoot, focusList( focus ) )
      focusList( focus )
    }
    def getComponentBefore( focusCycleRoot:Container, component:Component ) = {
      focus = (focusList.length + focus - 1) % focusList.length
      
      if ( !focusList( focus ).isEnabled ) getComponentBefore( focusCycleRoot, focusList( focus ) )
      focusList( focus )
    }
    def getDefaultComponent( focusCycleRoot:Container ) = if ( usernameField.text != "" ) focusList( 1 ) else focusList( 0 ) 
    def getLastComponent( focusCycleRoot:Container ) = focusList.last
    def getFirstComponent( focusCycleRoot:Container ) = focusList.first
  })
  
  title = "Login"
  modal = true
  
  listenTo( usernameField.keys, passwordField.keys, okButton.keys, cancelButton.keys )
  reactions += {
    case KeyPressed( _, Key.Escape, _, _ ) => cancelButton.doClick
    case KeyPressed( _, Key.Enter, _, _ ) => okButton.doClick
  }
  
  contents = panel
  pack
  centerOnScreen
}